package com.ruoyi.record.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.record.service.IRecordBenefitService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.record.domain.RecordBenefitContent;
import com.ruoyi.record.service.IRecordBenefitContentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 保险金内容Controller
 * 
 * @author ruoyi
 * @date 2024-02-19
 */
@Controller
@RequestMapping("/record/benefit/content")
public class RecordBenefitContentController extends BaseController
{
    private String prefix = "record/benefit/content";

    @Autowired
    private IRecordBenefitService recordBenefitService;

    @Autowired
    private IRecordBenefitContentService recordBenefitContentService;

    @RequiresPermissions("record:benefit:view")
    @GetMapping("/{benefitId}")
    public String content(@PathVariable("benefitId") Long benefitId, ModelMap mmap)
    {
        mmap.put("benefitId", benefitId);
        return prefix + "/content";
    }

    /**
     * 查询保险金内容列表
     */
    @RequiresPermissions("record:benefit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordBenefitContent recordBenefitContent)
    {
        startPage();
        List<RecordBenefitContent> list = recordBenefitContentService.selectRecordBenefitContentList(recordBenefitContent);
        return getDataTable(list);
    }

    /**
     * 导入保险金详情
     * @param file
     * @param benefitId
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "保险金详情", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, Long benefitId, boolean updateSupport) throws Exception
    {
        ExcelUtil<RecordBenefitContent> util = new ExcelUtil<RecordBenefitContent>(RecordBenefitContent.class);
        List<RecordBenefitContent> contentList = BeanUtils.copyList(util.importExcel(file.getInputStream()), RecordBenefitContent::new);
        String message = recordBenefitContentService.importContent(contentList, benefitId, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<RecordBenefitContent> util = new ExcelUtil<RecordBenefitContent>(RecordBenefitContent.class);
        return util.importTemplateExcel("保险金内容");
    }

    /**
     * 导出保险金内容列表
     */
    @RequiresPermissions("record:benefit:export")
    @Log(title = "保险金内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecordBenefitContent recordBenefitContent)
    {
        List<RecordBenefitContent> list = recordBenefitContentService.selectRecordBenefitContentList(recordBenefitContent);
        ExcelUtil<RecordBenefitContent> util = new ExcelUtil<RecordBenefitContent>(RecordBenefitContent.class);
        return util.exportExcel(list, "保险金内容");
    }

    /**
     * 新增保险金内容
     */
    @GetMapping("/add/{benefitId}")
    public String add(@PathVariable("benefitId") Long benefitId, ModelMap mmap)
    {
        mmap.put("benefitId", benefitId);
        return prefix + "/add";
    }

    /**
     * 新增保存保险金内容
     */
    @RequiresPermissions("record:benefit:add")
    @Log(title = "保险金内容", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordBenefitContent recordBenefitContent)
    {
        recordBenefitContent.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recordBenefitContentService.insertRecordBenefitContent(recordBenefitContent));
    }

    /**
     * 修改保险金内容
     */
    @GetMapping("/edit/{contentId}")
    public String edit(@PathVariable("contentId") Long contentId, ModelMap mmap)
    {
        RecordBenefitContent recordBenefitContent = recordBenefitContentService.selectRecordBenefitContentByContentId(contentId);
        mmap.put("recordBenefitContent", recordBenefitContent);
        return prefix + "/edit";
    }

    /**
     * 修改保存保险金内容
     */
    @RequiresPermissions("record:benefit:edit")
    @Log(title = "保险金内容", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordBenefitContent recordBenefitContent)
    {
        recordBenefitContent.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recordBenefitContentService.updateRecordBenefitContent(recordBenefitContent));
    }

    /**
     * 删除保险金内容
     */
    @RequiresPermissions("record:benefit:remove")
    @Log(title = "保险金内容", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordBenefitContentService.deleteRecordBenefitContentByContentIds(ids));
    }

    /**
     * 校验员工是否存在工资记录
     */
    @PostMapping("/checkBenefitContent")
    @ResponseBody
    public AjaxResult checkBenefitContent(RecordBenefitContent recordBenefitContent)
    {
        boolean checkFlag = recordBenefitContentService.checkBenefitContent(recordBenefitContent);
        return checkFlag ? success() : error();
    }
}
